//
//  YDBuySelectedRateView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/15.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDBuySelectedRateView.h"
#import "YDBuySelectedRateCell.h"
#import "YDLossRateModel.h"

#define kAnimationTime 0.25
@interface YDBuySelectedRateView()<UITableViewDelegate , UITableViewDataSource>

//tableView
@property (nonatomic , weak) UITableView *tableView;


@end

@implementation YDBuySelectedRateView

-(void)setModelArray:(NSMutableArray *)modelArray
{
    _modelArray = modelArray;
    
    //刷新界面
}

- (void)setCurrentLoseRate:(NSString *)currentLoseRate
{
    _currentLoseRate = currentLoseRate;
    [_modelArray enumerateObjectsUsingBlock:^(YDLossRateModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([currentLoseRate isEqualToString:obj.point]) {
            obj.isSelected = YES;
        }else
        {
            obj.isSelected = NO;
        }
    }];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIColor *color = [UIColor clearColor];
        self.backgroundColor = [color colorWithAlphaComponent:0.5];
        
        
        UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
        dismissButton.backgroundColor = [UIColor clearColor];
        dismissButton.frame = CGRectMake(0, 0, LKScreenW, LKScreenH);
        [self addSubview:dismissButton];
        [dismissButton addTarget:self action:@selector(dismissButtonClicked) forControlEvents:UIControlEventTouchDown];
    
        [self tableView];
        
    }
    return self;
}

-(void)dismissButtonClicked
{
    [UIView animateWithDuration: kAnimationTime delay: 0.1 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
        self.tableView.y = LKScreenH;
    } completion: ^(BOOL finished) {
        if ([self.delegate respondsToSelector:@selector(buySelectedRateViewDismissButtonClick)]) {
            
            [self.delegate buySelectedRateViewDismissButtonClick];
        }
    }];
}

+(instancetype)buySelectedRateViewWithModelArray:(NSMutableArray *)modelArray
{
    YDBuySelectedRateView *rateView = [[self alloc] init];
    rateView.modelArray = modelArray;
    return rateView;
}
- (void)didMoveToSuperview
{
    [super didMoveToSuperview];

    //iphonex tabar 49 -> 83
    CGFloat tableViewH = isPhoneX ? self.modelArray.count * rateX812(50) + 34 : self.modelArray.count *rate750(50);
    
    if (tableViewH > LKScreenH / 3) {
        tableViewH = LKScreenH / 3;
    }
    
    self.tableView.height = tableViewH;
    
    [UIView animateWithDuration: kAnimationTime delay: 0.1 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
        self.tableView.y = LKScreenH - tableViewH;
    } completion: ^(BOOL finished) {
        
    }];
    
}
-(UITableView *)tableView
{
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LKScreenH, LKScreenW, 0) style:UITableViewStylePlain];
        [self addSubview:tableView];
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.showsHorizontalScrollIndicator = NO;
        
        _tableView = tableView;
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(50) : rate750(50);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDBuySelectedRateCell *cell = [YDBuySelectedRateCell cellWithTableView:tableView];
    YDLossRateModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDLossRateModel *model = self.modelArray[indexPath.row];
    
    [self.modelArray enumerateObjectsUsingBlock:^(YDLossRateModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqual:model]) {
            model.isSelected = YES;
        }else
        {
            obj.isSelected = NO;
        }
    }];
    
    
    [self.tableView reloadData];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedRateString:)]) {
        [self.delegate didSelectedRateString:model.point];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kAnimationTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        DDLog(@"选择了某一个利率")
        
        [self dismissButtonClicked];
        
    });
    
    
}

-(void)dealloc
{
    DDLog(@"释放了")
}

@end
